﻿using RuoyiQuickLogin.Core;

var baseUrl = "http://127.0.0.1:8080";
var username = "admin";
var password = "admin123";

var length = args.Length;

if(length > 0)
{
    if(args[0] == "?")
    {
        Console.WriteLine("默认参数：\n" +
            "\tUrl: http://127.0.0.1:8080\n" +
            "\tusername: admin\n" +
            "\tpassword: admin123\n");
        Console.WriteLine("程序参数：\n" +
            "\t--url [url] 设置url\n" +
            "\t--u [name] 设置username\n" +
            "\t--p [pwd] 设置password\n");
    }

    if(length % 2 == 1)
    {
        Console.WriteLine("输入参数有误，请检查。");
        return;
    }

    var i = 0;
    while(i < length)
    {
        var arg = args[i].Trim('-').ToLower();
        switch (arg)
        {
            case "url":
                baseUrl = args[i+1];
                break;
            case "u":
            case "username":
                username = args[i+1];
                break;
            case "p":
            case"password":
                password = args[i+1];
                break;
            default:
                Console.WriteLine($"输入参数有误，不存在参数\"{arg}\"。");
                break;
        }
        i += 2;
    }
}

Console.WriteLine("程序执行参数：\n" +
            $"\tUrl: {baseUrl}\n" +
            $"\tusername: {username}\n" +
            $"\tpassword: {password}\n");

var http = new RuoyiHttp();
http.BaseUrl = baseUrl;
try
{
    http.Login(username, password);
}
catch (Exception _)
{
    Console.WriteLine($"程序执行失败，请检查ruoyi与Redis是否已启动。");
    Console.ReadLine();
    return;
}
Console.WriteLine($"程序执行成功。\nAuthorization");
Console.WriteLine(http.Token);
Console.ReadLine();